/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.data.Airline;
import easybooking.data.Plane;
import easybooking.data.Type;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**
 *
 * @author tim
 */
public class PlaneDialog extends JDialog {

     private EBInterface ebi;
    private JLabel lblName;
    private JTextField txfName;
    private JPanel dialogPanel;
    private JPanel buttonPanel;
    private JButton btnOk;
    private JButton btnCancel;
    private Plane editablePlane;
    private Type t;
    private Airline airl;
    private JTextField txfType = new JTextField();
    private JLabel lblType;
    private JButton btnTypeSelect;
    private JLabel lblAirline;
    private JTextField txfAirline = new JTextField();
    private JButton btnAirlineSelect;

    public PlaneDialog(EBInterface ebif) {
        ebi = ebif;
        //generateComponents();
    }
    // Get Text Fields
/**
 * get the inserted text from the name text field which is the name of the plane
 * @return the inserted text
 */
    public String getName() {
        return txfName.getText();
    }
/**
 * gets the type of a plane
 * @return the type of the plane
 */
     public Type getType() {
        return t;
    }
/**
 * gets the airline of a plane
 * @return the airline of the plane
 */
    public Airline getAirline() {
        return airl;
    }

    
/**
 * get the editable plane object
 * @return the plane object which should be edit
 */
    public Plane getEditablePlane(){
        return editablePlane;
    }
/**
 * sets the type into the type text field
 * @param t is the type of the Plane
 */
    public void setType(Type t){
        try {
            this.t = t;
            txfType.setText(t.getName());
        } catch (RemoteException ex) {
            Logger.getLogger(PlaneDialog.class.getName()).log(Level.SEVERE, null, ex);
        }



    }
/**
 * sets the airline into the airline text field
 * @param airl is the airline of the Plane
 */
    public void setAirline (Airline airl){
        try {
            this.airl = airl;
            txfAirline.setText(airl.getName());
        } catch (RemoteException ex) {
            Logger.getLogger(PlaneDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

/**
 * sets the atributs of the plane into the text fields
 * @param p is the plane object which atributs get instert into the text fields
 */
    public void setTextFields(Plane p){
        try {
            txfName.setText(p.getName());
            //txfAirline.setText(String.valueOf(p.getAirlineId()));
            txfAirline.setText(airl.getName());
            txfType.setText(t.getName());
        } catch (RemoteException ex) {
            Logger.getLogger(PlaneDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //shows the dialog when needed
        /**
 * calls the generateComponents() method to create and show the planeDialog
 * @param p is the plane object which should be edit
 */
     public void createAndShowDialog(Plane p) {
        editablePlane = p;
        generateComponents();
        btnOk.setName("editPlane");
        setTextFields(p);
        this.setResizable(true);
        this.setTitle("Edit Plane");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }
     /**
 * calls the generateComponents() method to create and show the PlaneDialog
 */
     public void createAndShowDialog() {
        generateComponents();
        btnOk.setName("createPlane");
        this.setResizable(true);
        this.setTitle("Create Plane");
        this.setModal(true); // deaktivates input to other dialogs
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
        this.toFront();
    }

    // Preperes the dialog
              /**
 * Generates all components to create and display the GUI
 */
    private void generateComponents() {

        // Dialog Settings

        dialogPanel = new JPanel();


        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }



        dialogPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        lblName = new JLabel("Name:");
        addComponent(dialogPanel, gbl, lblName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);

        txfName = new JTextField(10);
        txfName.setDocument(new JTextFieldLimit(255));
        addComponent(dialogPanel, gbl, txfName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        lblType = new JLabel("Type:");
        addComponent(dialogPanel, gbl, lblType, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 1, 1, 1, 0, 0);

        txfType = new JTextField(10);
        txfType.setEditable(false);

        addComponent(dialogPanel, gbl, txfType, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 1, 1, 1, 0, 0);

        btnTypeSelect = new JButton("Select");
        btnTypeSelect.setActionCommand("PlaneDialogTypeSelect");
        btnTypeSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnTypeSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 1, 1, 1, 0, 0);


        lblAirline = new JLabel("Airline:");
        addComponent(dialogPanel, gbl, lblAirline, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 1, 1, 0, 0);

        txfAirline = new JTextField(10);
        txfAirline.setEditable(false);
        addComponent(dialogPanel, gbl, txfAirline, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 2, 1, 1, 0, 0);

        btnAirlineSelect = new JButton("Select");
        btnAirlineSelect.setActionCommand("PlaneDialogAirlineSelect");
        btnAirlineSelect.addActionListener(ebi);
        addComponent(dialogPanel, gbl, btnAirlineSelect, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 2, 2, 1, 1, 0, 0);

        // adds to buttonPanel
        addComponent(dialogPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 6, 2, 1, 0, 0);

        // sets die dialogPanel as contentPane
        this.setContentPane(dialogPanel);


    }
    

    // Creates the button panel
        /**
 * Creates the buttons to commit with ok or cancel
 * @return a JPanel with 2 buttons
 */
    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();

        btnOk = new JButton("OK");
        btnCancel = new JButton("Cancel");

        // Sets the buttons the same size
        btnOk.setPreferredSize(btnCancel.getPreferredSize());

        // Registrate action listeners
        btnOk.setActionCommand("planeDialogOk");
        btnOk.addActionListener(ebi);

        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // Adds the buttons to the buttonPanel
        buttonPanel.add(btnOk);
        buttonPanel.add(btnCancel);
        return buttonPanel;
    }
/**
 * defines the layout of the GUI
 * @param cont container to which the component is added
 * @param gbl GridBagLayout object
 * @param c compontent which is added to the container
 * @param fill how to use free space in the cell around the component
 * @param anchor where to fix an align the component
 * @param x x position in the grid layout
 * @param y y position in the grid layout
 * @param width number of columns the element should spread
 * @param hight number of rows the element should spread
 * @param weightx handels the horizontal free space
 * @param weighty handels the vertical free space
 */
    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
